/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Lesson;
import dao.CourseDao;
import classes.HibernateUtil;
import dao.LessonDao;

/**
 *This servlet prepares the lesson.jsp and gets a list of lessons depending on the courses
 * @author Michel
 */
public class MyLessonController extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("id") != null) { //Edit lesson
            editLesson(Long.parseLong(request.getParameter("id")), request, response);
        } else if (request.getParameter("idremove") != null) { //remove lesson
            removeLesson(Long.parseLong(request.getParameter("idremove")), request, response);
        } else { //Get the Lesson list and redirect to the Lesson page

            getLessonList(request);
            
            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("CourseLessonEdit", "Manage lessons"),
                new Breadcrumbs("ManageLesson?courseid", "Add lesson")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);
            
            RequestDispatcher dispatcher = request.getRequestDispatcher("lesson.jsp");
            dispatcher.forward(request, response);
        }
    }


    public void editLesson(long id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get all the information about the teacher to fill in the form.
        HibernateUtil hu = new HibernateUtil();
        LessonDao ed = new LessonDao(hu.openSession());

        List<Lesson> LessonList = ed.findAll();
        Lesson temp = null;
        for (int i = 0; i < LessonList.size(); i++) {
            if (LessonList.get(i).getId() == id) {
                temp = LessonList.get(i); //get the Lesson from the list and attach it to the temporary object
            }
        }

        if (temp != null) {
            request.setAttribute("Lessonedit", temp);
        } else {
            System.out.println("The id you selected does not exist to modify.");
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_lessons.jsp");
        dispatcher.forward(request, response);
    }

    public void removeLesson(long id, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        LessonDao ld = new LessonDao(hu.openSession());

        ld.delete(ld.get(id));
        hu.close();

        response.sendRedirect("../Lesson");
    }
    // sets the list of Lessons 

    public void getLessonList(HttpServletRequest request) {
        HibernateUtil hu = new HibernateUtil();
        LessonDao ld = new LessonDao(hu.openSession());

        List<Lesson> LessonList = ld.findAll();

        request.setAttribute("Lessonlist", LessonList);
        hu.close();
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
